<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/10/9
 * Time: 下午5:33
 */
class OrderProduct extends Eloquent
{
    protected $table = "order_product";

    public static function getOrderProductForEngineer($order_id,$engineer_data){
        $products = array();
        $promotion_product = array();
        $base_product = array();
        $non_standard_product = array();
        $enterprise_product = array();
        $activity_product = array();
        $sc_product = array();
        $product_data = OrderProduct::where('order_id',$order_id)
            ->where('status',1)
            ->orderBy('product_type', 'desc')
            ->get()->toArray();
        foreach($product_data as $_product){
            if($_product['product_type']!='promotion'){
                //获取技师信息
                if($_product['engineer_id']==0){
                    $me=$engineer_data;
                }else{
                    $me=MaintenanceEngineers::find($_product['engineer_id']);
                }
            }
            $me=isset($me)?$me:array('id'=>'','name'=>'');
            if($_product['product_type'] == 'promotion') { //推广产品
                $_promotion_product = OrderProduct::getOrderPromotionProductPackage($order_id,$_product['id'],$engineer_data);
                foreach($_promotion_product as $pro){
                    array_push($promotion_product,$pro);
                }
                array_push($promotion_product,$_promotion_product);
            }elseif($_product['product_type'] == 'enterprise') { //企业套餐
                $_enterprise_product = OrderProduct::getOrderEnterpriseProduct($order_id,$_product['id'],$engineer_data);
                foreach($_enterprise_product as $es){
                    array_push($enterprise_product,$es);
                }
            }elseif($_product['product_type'] == 'activity') { //企业套餐
                $_activity_product = OrderProduct::getOrderActivityProduct($order_id,$_product['id'],$engineer_data);
                foreach($_activity_product as $pa){
                    array_push($activity_product,$pa);
                }
            }elseif($_product['product_type'] == 'base') { //基础产品
                $item = OrderProduct::getOrderProductItemBaseForEngineer($order_id,$_product['id']);
                $_base_product = array(
                    'id' => $_product['id'],
                    'package_name' => $_product['product_name'],
                    'type' => 'base',
                    'engineer_id' => $me['id'],
                    'engineer_name' => $me['name'],
                    'item' =>$item,

                );
                array_push($base_product,$_base_product);
            }elseif($_product['product_type'] == 'non_standard') { //非标产品
                $item = OrderProduct::getOrderProductNonStandardForEngineer($order_id,$_product['id']);
                $_non_standard_product = array(
                    'id' => $_product['id'],
                    'package_name' => $_product['product_name'],
                    'type' => 'non_standard',
                    'engineer_id' => $me['id'],
                    'engineer_name' => $me['name'],
                    'item' =>$item,

                );
                array_push($non_standard_product,$_non_standard_product);
            }elseif($_product['product_type'] == 'service_card') { //非标产品
                $item = OrderProduct::getOrderProductItemServiceForEngineer($order_id,$_product['id']);
                $sc_ = array(
                    'id' => $_product['id'],
                    'package_name' => $_product['product_name'],
                    'type' => 'base',
                    'engineer_id' => $me['id'],
                    'engineer_name' => $me['name'],
                    'item' =>$item,

                );
                array_push($sc_product,$sc_);
            }
        }
        if(count($promotion_product) > 0){
            foreach($promotion_product as $_promotion_product){
                foreach($_promotion_product as $__promotion_product)
                    array_push($products,$__promotion_product);
            }
        }
        if(count($base_product) > 0){
            foreach($base_product as $_base_product){
                array_push($products,$_base_product);
            }
        }
        if(count($enterprise_product) > 0){
            foreach($enterprise_product as $_enterprise_product){
                array_push($products,$_enterprise_product);
            }
        }
        if(count($activity_product) > 0){
            foreach($activity_product as $_activity_product){
                array_push($products,$_activity_product);
            }
        }
        if(count($non_standard_product) > 0){
            foreach($non_standard_product as $_non_standard_product){
                array_push($products,$_non_standard_product);
            }
        }
        if(count($sc_product) > 0){
            foreach($sc_product as $_sc){
                array_push($products,$_sc);
            }
        }
        return $products;
    }

    public static function getOrderPromotionProductPackage($order_id,$product_id,$engineer_data){
        $package = array();
        $package_data = OrderProductPackage::where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->get()->toArray();
        foreach($package_data as $_package){
            //获取技师信息
            if($_package['engineer_id']==0){
                $me=$engineer_data;
            }else{
                $me=MaintenanceEngineers::find($_package['engineer_id']);
            }
            $item = OrderProduct::getOrderPromotionProductItem($order_id,$_package['id']);
            $package_one = array(
                'id' => $_package['id'],
                'type' => 'promotion',
                'package_name' => $_package['package_name'],
                'engineer_id' => $me['id'],
                'engineer_name' => $me['name'],
                'item' => $item,

            );
            array_push($package,$package_one);
        }
        return $package;
    }
    
    public static function getOrderEnterpriseProduct($order_id,$product_id,$engineer_data){
        $package = array();
        $package_data = OrderProductEsPackage::where('order_id',$order_id)
        ->where('order_product_id',$product_id)
        ->get()->toArray();
        foreach($package_data as $_package){
            //获取技师信息
            if($_package['engineer_id']==0){
                $me=$engineer_data;
            }else{
                $me=MaintenanceEngineers::find($_package['engineer_id']);
            }
            $item = OrderProduct::getOrderEnterpriseProductItem($order_id,$_package['id']);
            $package_one = array(
                'id' => $_package['id'],
                'type' => 'enterprise',
                'package_name' => $_package['package_name'],
                'engineer_id' => $me['id'],
                'engineer_name' => $me['name'],
                'item' => $item,
        
            );
            array_push($package,$package_one);
        }
        return $package;
    }
    
    public static function getOrderActivityProduct($order_id,$product_id,$engineer_data){
        $package = array();
        $package_data = OrderProductAcPackage::where('order_id',$order_id)
        ->where('order_product_id',$product_id)
        ->get()->toArray();
        foreach($package_data as $_package){
            //获取技师信息
            if($_package['engineer_id']==0){
                $me=$engineer_data;
            }else{
                $me=MaintenanceEngineers::find($_package['engineer_id']);
            }
            $item = OrderProduct::getOrderActivityProductItem($order_id,$_package['id']);
            $package_one = array(
                'id' => $_package['id'],
                'type' => 'activity',
                'package_name' => $_package['package_name'],
                'engineer_id' => $me['id'],
                'engineer_name' => $me['name'],
                'item' => $item,
    
            );
            array_push($package,$package_one);
        }
        return $package;
    }
    public static function getOrderEnterpriseProductItem($order_id,$package_id){
        $item = OrderProductEsItem::where('order_id',$order_id)
        ->where('order_product_package_id',$package_id)
        ->where('type',1)
        ->select('order_product_esitem.id','order_product_esitem.item_id','order_product_esitem.item_name','order_product_esitem.num','order_product_esitem.item_type','order_product_esitem.is_adapter')
        ->get()->toArray();
        foreach($item as $key=>$value){
            if($value['is_adapter']==2){
                $item[$key]['subject_name']="";
                $item[$key]['brand_name']="";
                $item[$key]['series_name']="";
                $item[$key]['specification']="";
                $item[$key]['model']="请前台确认耗材";
            }else{
                // if($value['item_type']==0){
                    $si=SupplyItem::getCacheItemInfo($value['item_id']);
                    $item[$key]['subject_name']=$si->scf_name;
                    $item[$key]['brand_name']=$si->brand_name;
                    $item[$key]['series_name']=$si->series_name;
                    $item[$key]['specification']=$si->specification;
                    $item[$key]['model']=$si->model;
                // }else{
                //     $sins=SupplyItemNonStandard::getCacheItemInfo($value['item_id']);
                //     $item[$key]['subject_name']=$sins->scf_name;
                //     $item[$key]['brand_name']=$sins->brand_name;
                //     $item[$key]['series_name']=$sins->series_name;
                //     $item[$key]['specification']=$sins->specification;
                //     $item[$key]['model']=$sins->model;
                // }
            }
            
        }
        return $item;
    }

    
    public static function getOrderActivityProductItem($order_id,$package_id){
        $item = OrderProductAcItem::where('order_id',$order_id)
        ->where('order_product_package_id',$package_id)
        ->where('type',1)
        ->select('order_product_acitem.id','order_product_acitem.item_id','order_product_acitem.item_name','order_product_acitem.num','order_product_acitem.item_type','order_product_acitem.is_adapter')
        ->get()->toArray();
        foreach($item as $key=>$value){
            if($value['is_adapter']==2){
                $item[$key]['subject_name']="";
                $item[$key]['brand_name']="";
                $item[$key]['series_name']="";
                $item[$key]['specification']="";
                $item[$key]['model']="请前台确认耗材";
            }else{
                // if($value['item_type']==0){
                    $si=SupplyItem::getCacheItemInfo($value['item_id']);
                    $item[$key]['subject_name']=$si->scf_name;
                    $item[$key]['brand_name']=$si->brand_name;
                    $item[$key]['series_name']=$si->series_name;
                    $item[$key]['specification']=$si->specification;
                    $item[$key]['model']=$si->model;
                // }else{
                //     $sins=SupplyItemNonStandard::getCacheItemInfo($value['item_id']);
                //     $item[$key]['subject_name']=$sins->scf_name;
                //     $item[$key]['brand_name']=$sins->brand_name;
                //     $item[$key]['series_name']=$sins->series_name;
                //     $item[$key]['specification']=$sins->specification;
                //     $item[$key]['model']=$sins->model;
                // }
            }            
        }
        return $item;
    }
    
    public static function getOrderPromotionProductItem($order_id,$package_id){
        $item = OrderProductItem::where('order_id',$order_id)
            ->where('order_product_package_id',$package_id)
            ->where('type',1)
            ->select('order_product_item.id','order_product_item.item_id','order_product_item.item_name','order_product_item.num','order_product_item.item_type')
            ->get()->toArray();
        foreach($item as $key=>$value){
            $si=SupplyItem::getCacheItemInfo($value['item_id']);
            $item[$key]['subject_name']=$si->scf_name;
            $item[$key]['brand_name']=$si->brand_name;
            $item[$key]['series_name']=$si->series_name;
            $item[$key]['specification']=$si->specification;
            $item[$key]['model']=$si->model;
        }
        return $item;
    }

    public static function getOrderProductNonStandardForEngineer($order_id,$product_id){
        $item = $item = OrderProductNonStandard::select('order_product_non_standard.id','order_product_non_standard.item_id','order_product_non_standard.name as item_name','order_product_non_standard.num','order_product_non_standard.type')
            ->where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->where('service_type',1)
            ->where('order_product_non_standard.status',1)
            ->get()->toArray();
        foreach($item as $key=>$value){
            // if($value['type']==0){
                $si=SupplyItem::getCacheItemInfo($value['item_id']);
                $item[$key]['subject_name']=$si->scf_name;
                $item[$key]['brand_name']=$si->brand_name;
                $item[$key]['series_name']=$si->series_name;
                $item[$key]['specification']=$si->specification;
                $item[$key]['model']=$si->model;
            // }else{
            //     $sins=SupplyItemNonStandard::getCacheItemInfo($value['item_id']);
            //     $item[$key]['subject_name']=$sins->scf_name;
            //     $item[$key]['brand_name']=$sins->brand_name;
            //     $item[$key]['series_name']=$sins->series_name;
            //     $item[$key]['specification']=$sins->specification;
            //     $item[$key]['model']=$sins->model;
            // }
        }
        return $item;
    }

    public static function getOrderProductItemBaseForEngineer($order_id,$product_id){
        $item = OrderProductBaseItem::select('order_product_base_item.id','order_product_base_item.item_id','order_product_base_item.item_name','order_product_base_item.num','order_product_base_item.item_type','order_product_base_item.is_adapter')
            ->where('order_id',$order_id)
            ->where('order_product_package_id',$product_id)
            ->where('order_product_base_item.type',1)
            ->get()->toArray();
        foreach($item as $key=>$value){
            // if($value['item_type']==0){
                if($value['is_adapter']==1){
                    $si=SupplyItem::getCacheItemInfo($value['item_id']);
                    $item[$key]['subject_name']=$si->scf_name;
                    $item[$key]['brand_name']=$si->brand_name;
                    $item[$key]['series_name']=$si->series_name;
                    $item[$key]['specification']=$si->specification;
                    $item[$key]['model']=$si->model;
                }else{
                    $item[$key]['subject_name']="";
                    $item[$key]['brand_name']="";
                    $item[$key]['series_name']="";
                    $item[$key]['specification']="";
                    $item[$key]['model']="请前台确认耗材";
                }
            // }else{
            //     $sins=SupplyItemNonStandard::getCacheItemInfo($value['item_id']);
            //     $item[$key]['subject_name']=$sins->scf_name;
            //     $item[$key]['brand_name']=$sins->brand_name;
            //     $item[$key]['series_name']=$sins->series_name;
            //     $item[$key]['specification']=$sins->specification;
            //     $item[$key]['model']=$sins->model;
            // }
        }

        return $item;
    }

    public static function getOrderProductItemServiceForEngineer($order_id,$product_id){
        $item = OrderProductScItem::select('order_product_sc_item.id','order_product_sc_item.item_id','order_product_sc_item.item_name','order_product_sc_item.num','order_product_sc_item.item_type','order_product_sc_item.is_adapter')
            ->where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->where('order_product_sc_item.type',1)
            ->get()->toArray();
        foreach($item as $key=>$value){
            // if($value['item_type']==0){
                if($value['is_adapter']==1){
                    $si=SupplyItem::getCacheItemInfo($value['item_id']);
                    $item[$key]['subject_name']=$si->scf_name;
                    $item[$key]['brand_name']=$si->brand_name;
                    $item[$key]['series_name']=$si->series_name;
                    $item[$key]['specification']=$si->specification;
                    $item[$key]['model']=$si->model;
                }else{
                    $item[$key]['subject_name']="";
                    $item[$key]['brand_name']="";
                    $item[$key]['series_name']="";
                    $item[$key]['specification']="";
                    $item[$key]['model']="请前台确认耗材";
                }
            // }else{
            //     $sins=SupplyItemNonStandard::getCacheItemInfo($value['item_id']);
            //     $item[$key]['subject_name']=$sins->scf_name;
            //     $item[$key]['brand_name']=$sins->brand_name;
            //     $item[$key]['series_name']=$sins->series_name;
            //     $item[$key]['specification']=$sins->specification;
            //     $item[$key]['model']=$sins->model;
            // }
        }
        return $item;
    }

    public static function getOrderProduct($order_id){
        $promotion_product = array();
        $base_product = array();
        $non_standard_product = array();
        $product_data = OrderProduct::where('order_id',$order_id)
            ->where('status',1)
            ->orderBy('product_type', 'desc')
            ->get()->toArray();
        foreach($product_data as $_product){
            if($_product['product_type'] == 'promotion'){ //推广产品
                $package = OrderProduct::getOrderProductPackage($order_id,$_product['id']);
                $promotion = array(
                    'id' => $_product['id'],
                    'product_id' => $_product['product_id'],
                    'product_name' => $_product['product_name'],
                    'packages' =>$package
                );
                array_push($promotion_product,$promotion);
            }elseif($_product['product_type'] == 'base') { //基础产品
                $item = OrderProduct::getOrderProductItemBase($order_id,$_product['id']);
                $base = array(
                    'id' => $_product['id'],
                    'product_id' => $_product['product_id'],
                    'product_name' => $_product['product_name'],
                    'item' =>$item
                );
                array_push($base_product,$base);
            }elseif($_product['product_type'] == 'non_standard') { //非标产品
                $non_standard = OrderProduct::getOrderProductNonStandard($order_id,$_product['id']);
                array_push($non_standard_product,$non_standard);
            }
        }
        $product = array(
            'promotion' => $promotion_product,
            'base' => $base_product,
            'non_standard' => $non_standard_product
        );
        return $product;
    }

    public static function getOrderProductItemBase($order_id,$product_id){
        $item = OrderProductBaseItem::where('order_id',$order_id)
            ->where('order_product_package_id',$product_id)
            ->where('type',1)
            ->select(array('id','item_id',"item_type",'item_name','amount','num'))
            ->get()->toArray();
        return $item;
    }

    public static function getOrderProductPackage($order_id,$product_id){
        $package = array();
        $package_data = OrderProductPackage::where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->get()->toArray();
        foreach($package_data as $_package){
            $item = OrderProduct::getOrderProductItem($order_id,$_package['id']);
            $package_one = array(
                'id' => $_package['id'],
                'package_id' => $_package['package_id'],
                'package_name' => $_package['package_name'],
                'work_station' => $_package['work_station'],
                'work_status' => $_package['work_status'],
                'item' => $item
            );
            array_push($package,$package_one);
        }
        return $package;
    }

    public static function getOrderProductItem($order_id,$package_id){
        $item = OrderProductItem::where('order_id',$order_id)
            ->where('order_product_package_id',$package_id)
            ->where('type',1)
            ->select(array('id','item_id','item_name','amount','num'))
            ->get()->toArray();
        return $item;
    }

    public static function getOrderProductNonStandard($order_id,$product_id){
        $non_standard = OrderProductNonStandard::where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->where('order_product_non_standard.status',1)
            ->where('service_type',1)
            ->select(array('id','name','amount','num','work_station','work_status','type'))
            ->get()->toArray();
        return $non_standard;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-01-07 10:39:12
     * @Description: 用于前端获取该订单 产品信息的 方法
     */
    public static function  getOrderProductById($order_id){
        $order=Orders::find($order_id);
        if($order->engineer_id==0){
            $engineer_name="无";
        }else{
            $me=MaintenanceEngineers::find($order->engineer_id);
            $engineer_name=$me->name;
            $engineers[$order->engineer_id]=$engineer_name;
        }
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->orderBy('product_type','desc')->get();
        $activity_product=$enterprise_product=$promotion_procut=$base_procut=$non_standard=$service_card=$remind=array();
        foreach ($order_product as $_op) {
            switch ($_op->product_type) {
                case 'enterprise':
                    $engineers=array();
                    $engineers[$order->engineer_id]=$engineer_name;
                    $product_row=0;
                    $falg=true;
                    $opep=OrderProductEsPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opep as $key => $value){
                        $temp_product=ProductPackage::find($value->package_id);
                        $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                        if($scf){//说明配置了该服务分类的分类提醒
                            if(!isset($remind[$scf->sc_id])){
                                $remind[$scf->sc_id]=$scf;
                            }
                        }
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // if($_item->item_type==0){
                                    if($_item->is_adapter==1){
                                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                        $_item->scf_name=$item_info->scf_name;
                                        $_item->subject=$item_info->subject;
                                        $_item->subject_name=$item_info->subject_name;
                                        $_item->brand_name=$item_info->brand_name;
                                        $_item->specification=$item_info->specification;
                                        $_item->series_name=$item_info->series_name;
                                    }else{
                                        $_item->scf_name="";
                                        $_item->subject=$_item->item_id;
                                        $_item->subject_name="";
                                        $_item->brand_name="";
                                        $_item->specification="";
                                    }                                    
                                // }else{
                                //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                //     $_item->scf_name=$item_info->scf_name;
                                //     $_item->subject=$item_info->subject;
                                //     $_item->subject_name=$item_info->subject_name;
                                //     $_item->brand_name=$item_info->brand_name;
                                //     $_item->specification=$item_info->specification;
                                //     $_item->series_name=$item_info->series_name;
                                // }
                                $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                            }else{
                                $_item->unit='个';
                            }
                
                            $moo=MaintenanceOutOrder::where("product_type","enterprise")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                            $_item->moo_status=isset($moo)?$moo->status:null;
                
                            if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                                $falg=false;
                            }
                            //协助前段处理金钱价格
                            $_item->amount=round($_item->amount/100,2);
                            $_item->counter_amount=round($_item->counter_amount/100,2);
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                            $engineers[$order->engineer_id]=$mes->name;
                        }
                        //协助前段处理金钱价格                        
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    $engineers=array_values($engineers);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $_op->package=$opep;
                    $_op->moo_status=$falg;
                    $_op->col=$product_row;
                    $_op->engineers=$engineers;
                    $enterprise_product[]=$_op;
                    break;
                case 'activity':
                    $engineers=array();
                    $engineers[$order->engineer_id]=$engineer_name;
                    $product_row=0;
                    $falg=true;
                    $opacp=OrderProductAcPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opacp as $key => $value){
                        $temp_product=ProductPackage::find($value->package_id);
                        $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                        if($scf){//说明配置了该服务分类的分类提醒
                            if(!isset($remind[$scf->sc_id])){
                                $remind[$scf->sc_id]=$scf;
                            }
                        }
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // if($_item->item_type==0){
                                    if($_item->is_adapter==1){
                                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                        $_item->scf_name=$item_info->scf_name;
                                        $_item->subject=$item_info->subject;
                                        $_item->subject_name=$item_info->subject_name;
                                        $_item->brand_name=$item_info->brand_name;
                                        $_item->specification=$item_info->specification;
                                        $_item->series_name=$item_info->series_name;
                                    }else{
                                        $_item->scf_name="";
                                        $_item->subject=$_item->item_id;
                                        $_item->subject_name="";
                                        $_item->brand_name="";
                                        $_item->specification="";
                                    }
                                // }else{
                                //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                //     $_item->scf_name=$item_info->scf_name;
                                //     $_item->subject=$item_info->subject;
                                //     $_item->subject_name=$item_info->subject_name;
                                //     $_item->brand_name=$item_info->brand_name;
                                //     $_item->specification=$item_info->specification;
                                //     $_item->series_name=$item_info->series_name;
                                // }

                                $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                            }else{
                                $_item->unit='个';
                            }
                            $_item->discount=is_null($_item->discount)?1:$_item->discount;
                            $moo=MaintenanceOutOrder::where("product_type","activity")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                            $_item->moo_status=isset($moo)?$moo->status:null;
                
                            if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                                $falg=false;
                            }
                            //协助前段处理金钱价格
                            $_item->amount=round($_item->amount/100,2);
                            $_item->counter_amount=round($_item->counter_amount/100,2);
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                            $engineers[$order->engineer_id]=$mes->name;
                        }
                        //协助前段处理金钱价格
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    $engineers=array_values($engineers);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);                    
                    // $_op->discount=$_op->discount/100;
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);
                    
                    $_op->package=$opacp;
                    $_op->moo_status=$falg;
                    $_op->col=$product_row;
                    $_op->engineers=$engineers;
                    //增加 活动包的类型
                    $pa = ProductActivity::find($_op->product_id);
                    $_op->amount_type=$pa->type;
                    $_op->is_member=$pa->is_member;
                    $activity_product[]=$_op;
                    break;
                case 'promotion':
                    $product_row=0;
                    $falg=true;
                    $opp=OrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key => $value){
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // $si=SupplyItem::find($_item->item_id);
                                // $sb=SupplyBrand::find($si->brand_id);
                                // $s_s=SupplySubject::find($si->subject);
                                // $ss=SupplySeries::find($si->series_id);
                                // $scf=StatisticalClassification::find($si->st_classifi_id);
                                $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                $_item->scf_name=$item_info->scf_name;
                                $_item->unit=$item_info->unit;
                                $_item->unit=$item_info->subject;
                                $_item->subject_name=$item_info->subject_name;
                                $_item->brand_name=$item_info->brand_name;
                                $_item->specification=$item_info->specification;                                
                            }else{
                                $_item->unit='个';
                            }

                            $moo=MaintenanceOutOrder::where("product_type","promotion")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                            $_item->moo_status=isset($moo)?$moo->status:null;

                            if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                                $falg=false;
                            }
                            //协助前段处理金钱价格
                            $_item->amount=round($_item->amount/100,2);
                            $_item->counter_amount=round($_item->counter_amount/100,2);
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                        }
                        //协助前段处理金钱价格
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    $_op->package=$opp;
                    $_op->moo_status=$falg;
                    $_op->col=$product_row;

                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $promotion_procut[]=$_op;
                    break;
                case 'base':
                    $temp_product=ProductPackage::find($_op->product_id);
                    $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                    if($scf){//说明配置了该服务分类的分类提醒
                        if(!isset($remind[$scf->sc_id])){
                            $remind[$scf->sc_id]=$scf;
                        }
                    }
                    $product_row=0;
                    $falg=true;
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op->id)
                        // ->leftJoin('supply_item','order_product_base_item.item_id','=','supply_item.id')
                        // ->leftJoin('supply_item_non_standard','order_product_base_item.item_id','=','supply_item_non_standard.id')
                        // ->select('order_product_base_item.*','supply_item.subject as sis','supply_item.st_classifi_id as si_sci','supply_item.brand_id as sibi','supply_item_non_standard.subject as sinss','supply_item_non_standard.st_classifi_id as sins_sci','supply_item_non_standard.brand_name')
                        ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            // if($_item->item_type==0){
                                if($_item->is_adapter==1){
                                    $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                    $_item->scf_name=$item_info->scf_name;
                                    $_item->subject=$item_info->subject;
                                    $_item->subject_name=$item_info->subject_name;
                                    $_item->brand_name=$item_info->brand_name;
                                    $_item->specification=$item_info->specification;
                                    $_item->series_name=$item_info->series_name;
                                    $_item->ischange=PriceBargaining::ischange($_item->item_id,$order->shop_id);
                                }else{
                                    $_item->scf_name="";
                                    $_item->subject=$_item->item_id;
                                    $_item->subject_name="";
                                    $_item->brand_name="";
                                    $_item->specification="";
                                    $_item->ischange=0;
                                }
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                            //     $_item->scf_name=$item_info->scf_name;
                            //     $_item->subject=$item_info->subject;
                            //     $_item->subject_name=$item_info->subject_name;
                            //     $_item->brand_name=$item_info->brand_name;
                            //     $_item->specification=$item_info->specification;
                            //     $_item->series_name=$item_info->series_name;
                            //     $_item->ischange=1;
                            // }
                            $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                        }else{
                            $_item->unit='个';
                        }
                        $moo=MaintenanceOutOrder::where("product_type","base")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                        $_item->moo_status=isset($moo)?$moo->status:null;
                        if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                            $falg=false;
                        }
                        //协助前段处理金钱价格
                        $_item->amount=round($_item->amount/100,2);
                        $_item->counter_amount=round($_item->counter_amount/100,2);
                    }
                    if($_op->engineer_id==0){
                        $_op->engineer_name=$engineer_name;
                    }else{
                        $mes=MaintenanceEngineers::find($_op->engineer_id);
                        $_op->engineer_name=$mes->name;
                    }
                    $_op->item=$opp;
                    $_op->moo_status=$falg;
                    $_op->col=sizeof($opp);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $base_procut[]=$_op;
                    break;
                case 'service_card':
                    $temp_product=ProductPackage::find($_op->product_id);
                    $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                    if($scf){//说明配置了该服务分类的分类提醒
                        if(!isset($remind[$scf->sc_id])){
                            $remind[$scf->sc_id]=$scf;
                        }
                    }
                    $product_row=0;
                    $falg=true;
                    $opp=OrderProductScItem::where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            // if($_item->item_type==0){
                                if($_item->is_adapter==1){
                                    $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                    $_item->scf_name=$item_info->scf_name;
                                    $_item->subject=$item_info->subject;
                                    $_item->subject_name=$item_info->subject_name;
                                    $_item->brand_name=$item_info->brand_name;
                                    $_item->specification=$item_info->specification;
                                    $_item->series_name=$item_info->series_name;
                                }else{
                                    $_item->scf_name="";
                                    $_item->subject=$_item->item_id;
                                    $_item->subject_name="";
                                    $_item->brand_name="";
                                    $_item->specification="";
                                }
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                            //     $_item->scf_name=$item_info->scf_name;
                            //     $_item->subject=$item_info->subject;
                            //     $_item->subject_name=$item_info->subject_name;
                            //     $_item->brand_name=$item_info->brand_name;
                            //     $_item->specification=$item_info->specification;
                            //     $_item->series_name=$item_info->series_name;
                            // }
                            $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                        }else{
                            $_item->unit='个';
                        }
                        $moo=MaintenanceOutOrder::where("product_type","service_card")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                        $_item->moo_status=isset($moo)?$moo->status:null;
                        if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                            $falg=false;
                        }
                        //协助前段处理金钱价格
                        $_item->amount=round($_item->amount/100,2);
                        $_item->counter_amount=round($_item->counter_amount/100,2);
                    }
                    if($_op->engineer_id==0){
                        $_op->engineer_name=$engineer_name;
                    }else{
                        $mes=MaintenanceEngineers::find($_op->engineer_id);
                        $_op->engineer_name=$mes->name;
                    }
                    $_op->item=$opp;
                    $_op->moo_status=$falg;
                    $_op->col=sizeof($opp);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $service_card[]=$_op;
                    break;
                case 'non_standard':
                    $temp_product=Service::find($_op->product_id);
                    $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                    if($scf){//说明配置了该服务分类的分类提醒
                        if(!isset($remind[$scf->sc_id])){
                            $remind[$scf->sc_id]=$scf;
                        }
                    }
                    $falg=true;
                    $opn=OrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)
                        ->orderBy('service_type','asc')
                        // ->leftJoin('supply_item','order_product_non_standard.item_id','=','supply_item.id')
                        // ->leftJoin('supply_item_non_standard','order_product_non_standard.item_id','=','supply_item_non_standard.id')
                        // ->select('order_product_non_standard.*','supply_item.subject as sis','supply_item.st_classifi_id as si_sci','supply_item.brand_id as sibi','supply_item_non_standard.subject as sinss','supply_item_non_standard.st_classifi_id as sins_sci','supply_item_non_standard.brand_name')
                        ->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            // if($value->type==0){
                                $item_info=SupplyItem::getCacheItemInfo($value->item_id);
                                $value->scf_name=$item_info->scf_name;
                                $value->subject=$item_info->subject;
                                $value->subject_name=$item_info->subject_name;
                                $value->brand_name=$item_info->brand_name;
                                $value->specification=$item_info->specification;
                                $value->series_name=$item_info->series_name;
                                $value->ischange=PriceBargaining::ischange($value->item_id,$order->shop_id);
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($value->item_id);
                            //     $value->scf_name=$item_info->scf_name;
                            //     $value->subject=$item_info->subject;
                            //     $value->subject_name=$item_info->subject_name;
                            //     $value->brand_name=$item_info->brand_name;
                            //     $value->specification=$item_info->specification;
                            //     $value->series_name=$item_info->series_name;
                            //     $value->ischange=1;
                            // }
                            $value->temp_subject=$value->subject;
                        }else{
                            $value->unit='个';
                        }

                        $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$value->id)->whereIn("status",array(0,1,2))->first();
                        $value->moo_status=isset($moo)?$moo->status:null;
                        if(in_array($value->moo_status,array(1,2))||$value->moo_status===0){
                            $falg=false;
                        }

                        //协助前段处理金钱价格
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    if($_op->engineer_id==0){
                        $_op->engineer_name=$engineer_name;
                    }else{
                        $mes=MaintenanceEngineers::find($_op->engineer_id);
                        $_op->engineer_name=$mes->name;
                    }
                    $_op->item=$opn;
                    $_op->moo_status=$falg;
                    $_op->col=sizeof($opn);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $non_standard[]=$_op;
                    break;
            }
        }
        ksort($remind);
        return array("activity_product"=>$activity_product,"enterprise_product"=>$enterprise_product,"promotion_procut"=>$promotion_procut,"base_procut"=>$base_procut,"non_standard"=>$non_standard,"service_card"=>$service_card,"remind"=>array_values($remind));
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-08-17 14:54:24
     * @Description: Description
     */
    public static function  getOrderProductByIdEngin($order_id){
        $order=Orders::find($order_id);
        if($order->engineer_id==0){
            $engineer_name="无";
        }else{
            $me=MaintenanceEngineers::find($order->engineer_id);
            $engineer_name=$me->name;
            $engineers[$order->engineer_id]=$engineer_name;
        }
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->orderBy('product_type','desc')->get();
        $activity_product=$enterprise_product=$promotion_procut=$base_procut=$non_standard=$service_card=array();
        foreach ($order_product as $_op) {
            if($_op->engineer_id==0){
                $_op->engineer_name=$engineer_name;
            }else{
                $mes=MaintenanceEngineers::find($_op->engineer_id);
                $_op->engineer_name=$mes->name;
                $engineers[$order->engineer_id]=$mes->name;
            }
            switch ($_op->product_type) {
                case 'enterprise':
                    $engineers=array();
                    $engineers[$order->engineer_id]=$engineer_name;
                    // $product_row=0;
                    // $falg=true;
                    $opep=OrderProductEsPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opep as $key => $value){
                        // $value->col=sizeof($value->item);
                        // $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // if($_item->item_type==0){
                                    if($_item->is_adapter==1){
                                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                        $_item->scf_name=$item_info->scf_name;
                                        $_item->subject=$item_info->subject;
                                        $_item->subject_name=$item_info->subject_name;
                                        $_item->brand_name=$item_info->brand_name;
                                        $_item->specification=$item_info->specification;
                                        $_item->series_name=$item_info->series_name;
                                    }else{
                                        $_item->scf_name="";
                                        $_item->subject=$_item->item_id;
                                        $_item->subject_name="";
                                        $_item->brand_name="";
                                        $_item->specification="";
                                    }                                    
                                // }else{
                                //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                //     $_item->scf_name=$item_info->scf_name;
                                //     $_item->subject=$item_info->subject;
                                //     $_item->subject_name=$item_info->subject_name;
                                //     $_item->brand_name=$item_info->brand_name;
                                //     $_item->specification=$item_info->specification;
                                //     $_item->series_name=$item_info->series_name;
                                // }
                                $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                                $osf=OrderSmyFilling::where("order_id",$order->order_id)->where("product_item_id",$_item->id)->where("product_type","enterprise")->where("status",1)->first();
                                $_item->filling_code=isset($osf)?$osf->code:'';
                                $_item->filling_code_number=isset($osf)?$osf->filling_number:0;

                            }else{
                                $_item->unit='个';
                            }
                
                            // $moo=MaintenanceOutOrder::where("product_type","enterprise")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                            // $_item->moo_status=isset($moo)?$moo->status:null;
                
                            // if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                            //     $falg=false;
                            // }
                            //协助前段处理金钱价格
                            $_item->amount=round($_item->amount/100,2);
                            $_item->counter_amount=round($_item->counter_amount/100,2);
                            $_item->discount=1;
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                            $engineers[$order->engineer_id]=$mes->name;
                        }
                        //协助前段处理金钱价格                        
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    $engineers=array_values($engineers);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $_op->pack=$opep;
                    // $_op->moo_status=$falg;
                    // $_op->col=$product_row;                    
                    $enterprise_product[]=$_op;
                    break;
                case 'activity':
                    $engineers=array();
                    $engineers[$order->engineer_id]=$engineer_name;
                    // $product_row=0;
                    // $falg=true;
                    $opacp=OrderProductAcPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opacp as $key => $value){
                        // $value->col=sizeof($value->item);
                        // $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // if($_item->item_type==0){
                                    if($_item->is_adapter==1){
                                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                        $_item->scf_name=$item_info->scf_name;
                                        $_item->subject=$item_info->subject;
                                        $_item->subject_name=$item_info->subject_name;
                                        $_item->brand_name=$item_info->brand_name;
                                        $_item->specification=$item_info->specification;
                                        $_item->series_name=$item_info->series_name;
                                    }else{
                                        $_item->scf_name="";
                                        $_item->subject=$_item->item_id;
                                        $_item->subject_name="";
                                        $_item->brand_name="";
                                        $_item->specification="";
                                    }
                                // }else{
                                //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                //     $_item->scf_name=$item_info->scf_name;
                                //     $_item->subject=$item_info->subject;
                                //     $_item->subject_name=$item_info->subject_name;
                                //     $_item->brand_name=$item_info->brand_name;
                                //     $_item->specification=$item_info->specification;
                                //     $_item->series_name=$item_info->series_name;
                                // }

                                $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                                $osf=OrderSmyFilling::where("order_id",$order->order_id)->where("product_item_id",$_item->id)->where("product_type","activity")->where("status",1)->first();
                                $_item->filling_code=isset($osf)?$osf->code:'';
                                $_item->filling_code_number=isset($osf)?$osf->filling_number:0;
                            }else{
                                $_item->unit='个';
                            }
                            $_item->discount=is_null($_item->discount)?1:$_item->discount;
                            // $moo=MaintenanceOutOrder::where("product_type","activity")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                            // $_item->moo_status=isset($moo)?$moo->status:null;
                
                            // if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                            //     $falg=false;
                            // }
                            //协助前段处理金钱价格
                            $_item->amount=round($_item->amount/100,2);
                            $_item->counter_amount=round($_item->counter_amount/100,2);
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                            $engineers[$order->engineer_id]=$mes->name;
                        }
                        //协助前段处理金钱价格
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    $engineers=array_values($engineers);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);                    
                    // $_op->discount=$_op->discount/100;
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);
                    
                    $_op->pack=$opacp;
                    // $_op->moo_status=$falg;
                    // $_op->col=$product_row;
                    //增加 活动包的类型
                    $pa = ProductActivity::find($_op->product_id);
                    $_op->amount_type=$pa->type;
                    $activity_product[]=$_op;
                    break;
                case 'promotion':
                    $product_row=0;
                    $falg=true;
                    $opp=OrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key => $value){
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // $si=SupplyItem::find($_item->item_id);
                                // $sb=SupplyBrand::find($si->brand_id);
                                // $s_s=SupplySubject::find($si->subject);
                                // $ss=SupplySeries::find($si->series_id);
                                // $scf=StatisticalClassification::find($si->st_classifi_id);
                                $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                $_item->scf_name=$item_info->scf_name;
                                $_item->unit=$item_info->unit;
                                $_item->unit=$item_info->subject;
                                $_item->subject_name=$item_info->subject_name;
                                $_item->brand_name=$item_info->brand_name;
                                $_item->specification=$item_info->specification;                                
                            }else{
                                $_item->unit='个';
                            }

                            $moo=MaintenanceOutOrder::where("product_type","promotion")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                            $_item->moo_status=isset($moo)?$moo->status:null;

                            if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                                $falg=false;
                            }
                            //协助前段处理金钱价格
                            $_item->amount=round($_item->amount/100,2);
                            $_item->counter_amount=round($_item->counter_amount/100,2);
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                        }
                        //协助前段处理金钱价格
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);
                    }
                    $_op->pack=$opp;
                    $_op->moo_status=$falg;
                    $_op->col=$product_row;

                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $promotion_procut[]=$_op;
                    break;
                case 'base':
                    // $product_row=0;
                    // $falg=true;
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op->id)
                        // ->leftJoin('supply_item','order_product_base_item.item_id','=','supply_item.id')
                        // ->leftJoin('supply_item_non_standard','order_product_base_item.item_id','=','supply_item_non_standard.id')
                        // ->select('order_product_base_item.*','supply_item.subject as sis','supply_item.st_classifi_id as si_sci','supply_item.brand_id as sibi','supply_item_non_standard.subject as sinss','supply_item_non_standard.st_classifi_id as sins_sci','supply_item_non_standard.brand_name')
                        ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            // if($_item->item_type==0){
                                if($_item->is_adapter==1){
                                    $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                    $_item->scf_name=$item_info->scf_name;
                                    $_item->subject=$item_info->subject;
                                    $_item->subject_name=$item_info->subject_name;
                                    $_item->brand_name=$item_info->brand_name;
                                    $_item->specification=$item_info->specification;
                                    $_item->series_name=$item_info->series_name;
                                }else{
                                    $_item->scf_name="";
                                    $_item->subject=$_item->item_id;
                                    $_item->subject_name="";
                                    $_item->brand_name="";
                                    $_item->specification="";
                                }
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                            //     $_item->scf_name=$item_info->scf_name;
                            //     $_item->subject=$item_info->subject;
                            //     $_item->subject_name=$item_info->subject_name;
                            //     $_item->brand_name=$item_info->brand_name;
                            //     $_item->specification=$item_info->specification;
                            //     $_item->series_name=$item_info->series_name;
                            // }
                            $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                            $osf=OrderSmyFilling::where("order_id",$order->order_id)->where("product_item_id",$_item->id)->where("product_type","base")->where("status",1)->first();
                            $_item->filling_code=isset($osf)?$osf->code:'';
                            $_item->filling_code_number=isset($osf)?$osf->filling_number:0;
                        }else{
                            $_item->unit='个';
                        }
                        // $moo=MaintenanceOutOrder::where("product_type","base")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                        // $_item->moo_status=isset($moo)?$moo->status:null;
                        // if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                        //     $falg=false;
                        // }
                        //协助前段处理金钱价格
                        $_item->amount=round($_item->amount/100,2);
                        $_item->counter_amount=round($_item->counter_amount/100,2);
                    }
                    $_op->item=$opp;
                    // $_op->moo_status=$falg;
                    // $_op->col=sizeof($opp);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $base_procut[]=$_op;
                    break;
                case 'service_card':
                    // $product_row=0;
                    // $falg=true;
                    $opp=OrderProductScItem::where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            // if($_item->item_type==0){
                                if($_item->is_adapter==1){
                                    $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                    $_item->scf_name=$item_info->scf_name;
                                    $_item->subject=$item_info->subject;
                                    $_item->subject_name=$item_info->subject_name;
                                    $_item->brand_name=$item_info->brand_name;
                                    $_item->specification=$item_info->specification;
                                    $_item->series_name=$item_info->series_name;
                                }else{
                                    $_item->scf_name="";
                                    $_item->subject=$_item->item_id;
                                    $_item->subject_name="";
                                    $_item->brand_name="";
                                    $_item->specification="";
                                }
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                            //     $_item->scf_name=$item_info->scf_name;
                            //     $_item->subject=$item_info->subject;
                            //     $_item->subject_name=$item_info->subject_name;
                            //     $_item->brand_name=$item_info->brand_name;
                            //     $_item->specification=$item_info->specification;
                            //     $_item->series_name=$item_info->series_name;
                            // }
                            $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                            $osf=OrderSmyFilling::where("order_id",$order->order_id)->where("product_item_id",$_item->id)->where("product_type","service_card")->where("status",1)->first();
                            $_item->filling_code=isset($osf)?$osf->code:'';
                            $_item->filling_code_number=isset($osf)?$osf->filling_number:0;
                        }else{
                            $_item->unit='个';
                        }
                        // $moo=MaintenanceOutOrder::where("product_type","service_card")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->first();
                        // $_item->moo_status=isset($moo)?$moo->status:null;
                        // if(in_array($_item->moo_status,array(1,2))||$_item->moo_status===0){
                        //     $falg=false;
                        // }
                        //协助前段处理金钱价格
                        $_item->amount=round($_item->amount/100,2);
                        $_item->counter_amount=round($_item->counter_amount/100,2);
                    }
                    $_op->item=$opp;
                    // $_op->moo_status=$falg;
                    // $_op->col=sizeof($opp);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $service_card[]=$_op;
                    break;
                case 'non_standard':
                    // $falg=true;
                    $opn=OrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)
                        ->orderBy('service_type','asc')
                        // ->leftJoin('supply_item','order_product_non_standard.item_id','=','supply_item.id')
                        // ->leftJoin('supply_item_non_standard','order_product_non_standard.item_id','=','supply_item_non_standard.id')
                        // ->select('order_product_non_standard.*','supply_item.subject as sis','supply_item.st_classifi_id as si_sci','supply_item.brand_id as sibi','supply_item_non_standard.subject as sinss','supply_item_non_standard.st_classifi_id as sins_sci','supply_item_non_standard.brand_name')
                        ->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            // if($value->type==0){
                                $item_info=SupplyItem::getCacheItemInfo($value->item_id);
                                $value->scf_name=$item_info->scf_name;
                                $value->subject=$item_info->subject;
                                $value->subject_name=$item_info->subject_name;
                                $value->brand_name=$item_info->brand_name;
                                $value->specification=$item_info->specification;
                                $value->series_name=$item_info->series_name;
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($value->item_id);
                            //     $value->scf_name=$item_info->scf_name;
                            //     $value->subject=$item_info->subject;
                            //     $value->subject_name=$item_info->subject_name;
                            //     $value->brand_name=$item_info->brand_name;
                            //     $value->specification=$item_info->specification;
                            //     $value->series_name=$item_info->series_name;
                            // }
                            $value->item_name=$value->name;
                            $value->temp_subject=$value->subject;
                            $osf=OrderSmyFilling::where("order_id",$order->order_id)->where("product_item_id",$value->id)->where("product_type","non_standard")->where("status",1)->first();
                            $value->filling_code=isset($osf)?$osf->code:'';
                            $value->filling_code_number=isset($osf)?$osf->filling_number:0;
                        }else{
                            $value->unit='个';
                        }

                        // $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$value->id)->whereIn("status",array(0,1,2))->first();
                        // $value->moo_status=isset($moo)?$moo->status:null;
                        // if(in_array($value->moo_status,array(1,2))||$value->moo_status===0){
                        //     $falg=false;
                        // }

                        //协助前段处理金钱价格
                        $value->amount=round($value->amount/100,2);
                        $value->counter_amount=round($value->counter_amount/100,2);

                        $value->item_type=$value->type;
                        $value->type=$value->service_type;
                        $value->is_adapter=1;
                    }
                    $_op->item=$opn;
                    // $_op->moo_status=$falg;
                    // $_op->col=sizeof($opn);
                    //协助前段处理金钱价格
                    $_op->amount=round($_op->amount/100,2);
                    $_op->counter_amount=round($_op->counter_amount/100,2);
                    $_op->coupon_discount=round($_op->coupon_discount/100,2);

                    $non_standard[]=$_op;
                    break;
            }
        }
        return array("activity_product"=>$activity_product,"enterprise_product"=>$enterprise_product,"promotion_procut"=>$promotion_procut,"base_procut"=>$base_procut,"non_standard"=>$non_standard,"service_card"=>$service_card);
    }

    /*
         * 删除基础产品
         * */
    public static function delBaseProduct($order_id,$product_id,$user_id){
        return DB::transaction(function () use ($order_id,$product_id,$user_id) {
            $order_product = OrderProduct::where('order_id',$order_id)->where('product_id',$product_id)->where('status',1)->first();
            $order_product->status=0;
            $order_product->save();

            $order = Orders::find($order_id);
            $order->amount=$order->amount - $order_product->amount;
            $order->save();

            $opr=new OrderProductRecord();
            $opr->order_id = $order->order_id;
            $opr->product_type = $order_product->product_type;
            $opr->operation_type = 'delete';
            $opr->num = 1;
            $opr->operation_grade = '一级';
            $opr->user_id = $user_id;
            $opr->user_type = 'car';
            $opr->operation_id = $order_product->id;
            $opr->name = $order_product->product_name;
            $opr->amount = $order_product->amount;
            $opr->save();
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id));
        });
    }

    /**
     * @Author:      yuanjie
     * @DateTime:    2016-03-28
     * @Description: 上门车 添加 基础产品的 方法
     */
    public static function saveBaseProductsForOds($package_arr,$type,$sc_user_id,$order_id){
        return DB::transaction(function () use ($package_arr,$type,$sc_user_id,$order_id) {
            $order = Orders::where("order_id",$order_id)->lockForUpdate()->first();
            $temp_amount=0;
            foreach ($package_arr as $_pa) {
                $order_product=new OrderProduct;
                $order_product->order_id = $order_id;
                $order_product->product_type = "base";
                $order_product->product_id = $_pa["product_id"];
                $order_product->product_name = $_pa["product_name"];
                $order_product->status = 1;
                $order_product->amount = $_pa["amount"];
                $order_product->save();
                //添加各个产品项
                foreach ($_pa["item"] as $_pi) {
                    $opi=new OrderProductBaseItem;
                    $opi->order_id=$order_id;
                    $opi->order_product_package_id=$order_product->id;
                    $opi->item_id=$_pi->item_id;
                    $opi->item_name=$_pi->item_name;
                    $opi->amount=$_pi->amount;
                    $opi->num=$_pi->num;
                    if($_pi->type == 'service'){
                        $opi->type = 2;
                    }else{
                        $opi->type = 1;
                    }
                    $opi->unit=$_pi->unit;
                    $opi->save();
                }
                //记录添加日志
                $opr=new OrderProductRecord();
                $opr->order_id=$order_id;
                $opr->product_type="base";
                $opr->operation_type='create';
                $opr->num=1;
                $opr->operation_grade='一级';
                $opr->user_id=$sc_user_id;
                $opr->user_type='sc';
                $opr->operation_id=$order_product->id;
                $opr->name=$order_product->product_name;
                $opr->amount=$order_product->amount;
                $opr->save();
                $temp_amount+=$order_product->amount;
            }
            $order->amount=$order->amount+$temp_amount;
            $order->save();
            return $order_id;
        });
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-01-20 12:56:41
     * @Description: 添加 基础产品的 方法
     */
    public static function saveBaseProducts($package_arr,$type,$sc_user_id,$order_id){
        return DB::transaction(function () use ($package_arr,$type,$sc_user_id,$order_id) {
            $order=Orders::where("order_id",$order_id)->lockForUpdate()->first();
            $temp_amount=0;
            foreach ($package_arr as $_pa) {
                // $coupon_info=CouponOrderInfo::where("order_id",$order->order_id)
                // ->where("coupon_order_info.status",1)
                // ->where("coupon_base_info.type",2)
                // ->leftJoin('coupon_base_info','coupon_order_info.coupon_base_info_id','=','coupon_base_info.id')
                // ->select('coupon_order_info.coupon_id','coupon_base_info.pack_id')
                // ->first();
                // if($coupon_info->pack_id==$_pa['product_id']){
                //     $res=Orders::changeCouponStatus($order->order_id,4,$coupon_info->coupon_id,$sc_user_id);
                //     if($res['code']>0){
                //         return $res;
                //     }
                // }
                $order_product=new OrderProduct;
                $order_product->order_id=$order_id;
                $order_product->product_type="base";
                $order_product->product_id=$_pa["product_id"];
                $order_product->product_name=$_pa["product_name"];
                $order_product->status=1;
                $order_product->amount=$_pa["amount"];
                $order_product->save();
                //添加各个产品项
                foreach ($_pa["item"] as $_pi) {
                    $opi=new OrderProductBaseItem;
                    $opi->order_id=$order_id;
                    $opi->order_product_package_id=$order_product->id;
                    $opi->item_id=$_pi["item_id"];
                    $opi->item_name=$_pi["item_name"];
                    $opi->amount=$_pi["amount"];
                    $opi->num=$_pi["num"];
                    $opi->type=$_pi["type"];
                    $opi->unit=$_pi["unit"];
                    $opi->is_adapter=$_pi["is_adapter"];
                    $opi->save();
                    //判断特殊油品
                    if($_pi["type"]==1){
                        if(in_array($_pi["item_id"],Config::get("smy.item_id"))){//如果新耗材是特殊油品
                            OrderSmyFilling::saveosf($order->shop_id,$_pi["item_id"],"base",$opi->id,$order->order_id);
                        } 
                    }
                    
                }
                
                //记录添加日志
                $opr=new OrderProductRecord();
                $opr->order_id=$order_id;
                $opr->product_type="base";
                $opr->operation_type='create';
                $opr->num=1;
                $opr->operation_grade='一级';
                $opr->user_id=$sc_user_id;
                $opr->user_type='sc';
                $opr->operation_id=$order_product->id;
                $opr->name=$order_product->product_name;
                $opr->amount=$order_product->amount;
                $opr->save();
            }
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($order_id);
            return $order_id;
        });
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-08-02 13:16:43
     * @Description: 添加 套餐卡产品的方法
     * 逻辑添加耗材的类型 记录套餐卡使用记录 消减套餐卡次数
     */
    public static function saveServiceCardProducts($package_arr,$sc_user_id,$order_id,$service_card_detail_pack_id){
        return DB::transaction(function () use ($package_arr,$sc_user_id,$order_id,$service_card_detail_pack_id) {
            $order=Orders::where("order_id",$order_id)->lockForUpdate()->first();
            $scdp=ServiceCardDetailPack::find($service_card_detail_pack_id);
            $temp_amount=0;
            // foreach ($package_arr as $_pa) {
            $order_product=new OrderProduct;
            $order_product->order_id=$order_id;
            $order_product->product_type="service_card";
            $order_product->product_id=$package_arr["product_id"];
            $order_product->product_name=$package_arr["product_name"];
            $order_product->status=1;
            $order_product->amount=$package_arr["amount"];
            $order_product->service_car_detail_pack_id=$service_card_detail_pack_id;
            $order_product->save();
            //添加各个产品项
            foreach ($package_arr["item"] as $_pi) {
                $opi=new OrderProductScItem;
                $opi->order_id=$order_id;
                $opi->order_product_id=$order_product->id;
                $opi->item_id=$_pi["item_id"];
                $opi->item_name=$_pi["item_name"];
                $opi->amount=$_pi["amount"];
                $opi->num=$_pi["num"];
                $opi->type=$_pi["type"];
                $opi->unit=$_pi["unit"];
                $opi->is_adapter=$_pi["is_adapter"];
                $opi->save();
                //判断特殊油品
                if($_pi["type"]==1){
                    if(in_array($_pi["item_id"],Config::get("smy.item_id"))){//如果新耗材是特殊油品
                        OrderSmyFilling::saveosf($order->shop_id,$_pi["item_id"],"service_card",$opi->id,$order->order_id);
                    } 
                }
            }                
            //记录添加日志
            $opr=new OrderProductRecord();
            $opr->order_id=$order_id;
            $opr->product_type="service_card";
            $opr->operation_type='create';
            $opr->num=1;
            $opr->operation_grade='一级';
            $opr->user_id=$sc_user_id;
            $opr->user_type='sc';
            $opr->operation_id=$order_product->id;
            $opr->name=$order_product->product_name;
            $opr->amount=$order_product->amount;
            $opr->save();

            //记录订单使用套餐卡记录
            $scc=new ServiceCardConsumption();
            $scc->order_id=$order_id;
            $scc->service_card_detail_pack_id=$scdp->id;
            $scc->service_card_detail_id=$scdp->service_card_detail_id;
            $scc->type=0;
            $scc->product_id=$order_product->id;
            $scc->status=4;
            $scc->save();
            //减少套餐卡ID
             DB::table("service_card_detail_pack")->where("id",$scdp->id)->decrement("curr_number");
            // }
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($order_id);
            return $order_id;
        });
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-01-25 15:29:20
     * @Description: 用于删除 一个完整的产品的方法  对出库入库进行判断 如果没有出入库 对订单进行操作 对产品项进行操作
     */
    public static function deleteProduct($order_product,$user_id){
        return DB::transaction(function () use ($order_product,$user_id) {
            $order=Orders::find($order_product->order_id);
            $discount=$temp_amount=0;
            switch ($order_product->product_type) {
                case 'promotion':
                    $arr=DB::select("SELECT opi.* FROM `order_product_item` opi
                                LEFT JOIN order_product_package opp on opi.order_product_package_id=opp.id
                                left join maintenance_out_order moo on opi.id=moo.product_item_id
                                where opi.order_id=? and opp.order_product_id=?
                                and moo.product_type='promotion' and moo.status in(0,1,2)",array($order_product->order_id,$order_product->id));
                    if(sizeof($arr)>0){
                        return Lang::get("scs_error.order_product_item_out_error_40008");
                    }                    
                    break;
                case 'base':
                    $arr=DB::select("SELECT * FROM order_product_base_item opbi
                                LEFT JOIN maintenance_out_order moo on opbi.id=moo.product_item_id
                                where opbi.order_id=? and opbi.order_product_package_id=?
                                and moo.product_type='base' and moo.status in(0,1,2)",array($order_product->order_id,$order_product->id));
                    if(sizeof($arr)>0){
                        return Lang::get("scs_error.order_product_item_out_error_40008");
                    }
                    //除去燃油加注码
                    $id_arr=OrderProductBaseItem::where("order_product_package_id",$order_product->id)->where("type",1)->where("item_type",0)->select("id")->get()->toArray();
                    OrderSmyFilling::where("order_id",$order_product->order_id)->whereIn("product_item_id",$id_arr)->where("product_type",'base')->update(array("status"=>0));
                    break;
                case 'non_standard':
                    $arr=DB::select("SELECT * from order_product_non_standard opns
                                LEFT JOIN maintenance_out_order moo on opns.id=moo.product_item_id
                                where opns.order_id=? and opns.order_product_id=?
                                and moo.product_type='non_standard' and moo.status in(0,1,2)",array($order_product->order_id,$order_product->id));
                    if(sizeof($arr)>0){
                        return Lang::get("scs_error.order_product_item_out_error_40008");
                    }
                    //除去燃油加注码
                    $id_arr=OrderProductNonStandard::where("order_product_id",$order_product->id)->where("service_type",1)->where("type",0)->select("id")->get()->toArray();
                    OrderSmyFilling::where("order_id",$order_product->order_id)->whereIn("product_item_id",$id_arr)->where("product_type",'non_standard')->update(array("status"=>0));
                    break; 
                case 'service_card':
                    $arr=DB::select("SELECT * from order_product_sc_item opns
                                LEFT JOIN maintenance_out_order moo on opns.id=moo.product_item_id
                                where opns.order_id=? and opns.order_product_id=?
                                and moo.product_type='service_card' and moo.status in(0,1,2)",array($order_product->order_id,$order_product->id));
                    if(sizeof($arr)>0){
                        return Lang::get("scs_error.order_product_item_out_error_40008");
                    }
                    //更新使用状态 
                    $scc=ServiceCardConsumption::where("order_id",$order_product->order_id)->where("product_id",$order_product->id)->where("type",0)->first();
                    $scc->status=2;
                    $scc->save();
                    //回退套餐卡次数
                    DB::table("service_card_detail_pack")->where("id",$scc->service_card_detail_pack_id)->increment("curr_number");

                    //除去燃油加注码
                    $id_arr=OrderProductScItem::where("order_product_id",$order_product->id)->where("type",1)->where("item_type",0)->select("id")->get()->toArray();
                    OrderSmyFilling::where("order_id",$order_product->order_id)->whereIn("product_item_id",$id_arr)->where("product_type",'service_card')->update(array("status"=>0));
                    break;            
                default:
                    return Lang::get("scs_error.order_product_type_error_40007");
                    break;
            }
            $order_product->status=0;
            $order_product->save();
            // $coupon_info=CouponOrderInfo::where("order_id",$order_product->order_id)->where("coupon_order_info.status",1)->first();
            // if(isset($coupon_info->coupon_id)){
            //     $res=Orders::changeCouponStatus($order_product->order_id,3,$coupon_info->coupon_id,$user_id);
            //     if($res['code']>0){
            //         return $res;
            //     }
            // }
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($order_product->order_id);

            $opr=new OrderProductRecord();
            $opr->order_id=$order->order_id;
            $opr->product_type=$order_product->product_type;
            $opr->operation_type='delete';
            $opr->num=1;
            $opr->operation_grade='一级';
            $opr->user_id=$user_id;
            $opr->user_type='sc';
            $opr->operation_id=$order_product->id;
            $opr->name=$order_product->product_name;
            $opr->amount=$order_product->amount;
            $opr->save();
            OrderPerformance::deleteProcut($order->order_id,$order_product->id);
            Coupons::check_coupons($order->order_id);
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id));
        });
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-6-28 16:43:06
     * @Description: 用于删除 一个完整的企业套餐的方法  对出库入库进行判断 如果没有出入库 对订单进行操作 对产品项进行操作
     */
    public static function deleteProducts($order_product,$user_id){
        return DB::transaction(function () use ($order_product,$user_id) {
            $order=Orders::find($order_product->order_id);
            switch ($order_product->product_type) {
                case 'enterprise':
                    $arr=DB::select("SELECT opesi.* FROM `order_product_esitem` opesi
                                LEFT JOIN order_product_espackage opep on opesi.order_product_package_id=opep.id
                                left join maintenance_out_order moo on opesi.id=moo.product_item_id
                                where opesi.order_id=? and opep.order_product_id=?
                                and moo.product_type='enterprise' and moo.status in(0,1,2)",array($order_product->order_id,$order_product->id));
                    if(sizeof($arr)>0){
                        return Lang::get("scs_error.order_product_item_out_error_40008");
                    }

                    //除去燃油加注码
                    $id_arr=OrderProductEsPackage::leftJoin("order_product_esitem","order_product_esitem.order_product_package_id","=","order_product_espackage.id")->where("order_product_espackage.order_product_id",$order_product->id)->where("order_product_esitem.type",1)->where("order_product_esitem.item_type",0)->select("order_product_esitem.id")->get()->toArray();
                    OrderSmyFilling::where("order_id",$order_product->order_id)->whereIn("product_item_id",$id_arr)->where("product_type",'enterprise')->update(array("status"=>0));
                    break;
                default:
                    return Lang::get("scs_error.order_product_type_error_40007");
                    break;
            }
    
            $order_product->status=0;
            $order_product->save();
    
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($order_product->order_id);
    
            $opr=new OrderProductRecord();
            $opr->order_id=$order->order_id;
            $opr->product_type=$order_product->product_type;
            $opr->operation_type='delete';
            $opr->num=1;
            $opr->operation_grade='一级';
            $opr->user_id=$user_id;
            $opr->user_type='sc';
            $opr->operation_id=$order_product->id;
            $opr->name=$order_product->product_name;
            $opr->amount=$order_product->amount;
            $opr->save();
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id));
        });
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-6-28 16:43:06
     * @Description: 用于删除 一个活动包的方法  对出库入库进行判断 如果没有出入库 对订单进行操作 对产品项进行操作
     */
    public static function deleteActivityProducts($order_product,$user_id){
        return DB::transaction(function () use ($order_product,$user_id) {
            $order=Orders::find($order_product->order_id);
            switch ($order_product->product_type) {
                case 'activity':
                    $arr=DB::select("SELECT opaci.* FROM `order_product_acitem` opaci
                                LEFT JOIN order_product_acpackage opacp on opaci.order_product_package_id=opacp.id
                                left join maintenance_out_order moo on opaci.id=moo.product_item_id
                                where opaci.order_id=? and opacp.order_product_id=?
                                and moo.product_type='activity' and moo.status in(0,1,2)",array($order_product->order_id,$order_product->id));
                    if(sizeof($arr)>0){
                        return Lang::get("scs_error.order_product_item_out_error_40008");
                    }
                     //除去燃油加注码
                    $id_arr=OrderProductAcPackage::leftJoin("order_product_acitem","order_product_acitem.order_product_package_id","=","order_product_acpackage.id")->where("order_product_acpackage.order_product_id",$order_product->id)->where("order_product_acitem.type",1)->where("order_product_acitem.item_type",0)->select("order_product_acitem.id")->get()->toArray();
                    OrderSmyFilling::where("order_id",$order_product->order_id)->whereIn("product_item_id",$id_arr)->where("product_type",'activity')->update(array("status"=>0));
                    break;
                default:
                    return Lang::get("scs_error.order_product_type_error_40007");
                    break;
            }
    
            $order_product->status=0;
            $order_product->save();
            
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($order_product->order_id);
    
            $opr=new OrderProductRecord();
            $opr->order_id=$order->order_id;
            $opr->product_type=$order_product->product_type;
            $opr->operation_type='delete';
            $opr->num=1;
            $opr->operation_grade='一级';
            $opr->user_id=$user_id;
            $opr->user_type='sc';
            $opr->operation_id=$order_product->id;
            $opr->name=$order_product->product_name;
            $opr->amount=$order_product->amount;
            $opr->save();
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id));
        });
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-3-7 17:45:15
     * @Description: 维修记录
     */
    /**
     * @Author:      lyt
     * @DateTime:  2017-08-01 15:16:30
     * @Description: 发现没用 注释掉该方法
     */
    // public static function  getOrderProductByOrderId($order_ids){
    //     $promotion_procut=$base_procut=$item=array();
    //     $order_ids = array_column($order_ids, 'order_id');
    //     $order_products=OrderProduct::whereIn("order_id",$order_ids)->where("status",1)->get()->toArray();
    //     foreach ($order_products as $_op) {
    //         $shop_name=Orders::leftJoin('maintenance_shops','orders.shop_id','=','maintenance_shops.id')->where('order_id',$_op['order_id'])->select('maintenance_shops.name')->first();
    //         switch ($_op['product_type']) {
    //             case 'enterprise':
    //                 $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
    //                 $opp_ids = array_column($opp_ids, 'id');
    //                 $items=OrderProductEsItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get()->toArray();
    //                 foreach ($items as $key=>$_item) {
    //                     if($_item->item_type==0){
    //                         $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
    //                         $_item->scf_name=$item_info->scf_name;
    //                         $_item->unit=$item_info->unit;
    //                         $_item->brand_name=$item_info->brand_name;
    //                         $_item->series_name=$item_info->series_name;
    //                         $_item->model=$item_info->model;
    //                         $_item->specification=$item_info->specification;
    //                         $_item->shop_name=$shop_name->name;
    //                     }else{
    //                         $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
    //                         $_item->scf_name=$item_info->scf_name;
    //                         $_item->unit=$item_info->unit;
    //                         $_item->brand_name=$item_info->brand_name;
    //                         $_item->series_name=$item_info->series_name;
    //                         $_item->model=$item_info->model;
    //                         $_item->specification=$item_info->specification;
    //                         $_item->shop_name=$shop_name->name;
    //                     }
    //                 }
    //                 break;
    //             case 'activity':
    //                 $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
    //                 $opp_ids = array_column($opp_ids, 'id');
    //                 $items=OrderProductAcItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get()->toArray();
    //                 foreach ($items as $key=>$_item) {
    //                     if($_item->item_type==0){
    //                         $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
    //                         $_item->scf_name=$item_info->scf_name;
    //                         $_item->unit=$item_info->unit;
    //                         $_item->brand_name=$item_info->brand_name;
    //                         $_item->series_name=$item_info->series_name;
    //                         $_item->model=$item_info->model;
    //                         $_item->specification=$item_info->specification;
    //                         $_item->shop_name=$shop_name->name;
    //                     }else{
    //                         $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
    //                         $_item->scf_name=$item_info->scf_name;
    //                         $_item->unit=$item_info->unit;
    //                         $_item->brand_name=$item_info->brand_name;
    //                         $_item->series_name=$item_info->series_name;
    //                         $_item->model=$item_info->model;
    //                         $_item->specification=$item_info->specification;
    //                         $_item->shop_name=$shop_name->name;
    //                     }
    //                 }
    //                 break;
    //             case 'promotion':
    //                 $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
    //                 $opp_ids = array_column($opp_ids, 'id');
    //                 $items=OrderProductItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get()->toArray();
    //                 foreach ($items as $key=>$_item) {         
    //                     $si=SupplyItem::find($_item['item_id']);
    //                     $ss=SupplySeries::find($si->series_id);
    //                     $_item['unit']=$ss->unit;
    //                     $_item['shop_name']=$shop_name->name;
    //                     $items[$key]['unit']=$ss->unit;
    //                     $items[$key]['shop_name']=$shop_name->name;
    //                     if(isset($promotion_procut[$_op['product_id']])){
    //                         $promotion_procut[$_op['product_id']]['items'][]=$_item;
    //                     }
    //                     if(isset($item)){
    //                         $item['promotion'][]=$_item;
    //                     }
    //                 }
    //                 if(!isset($promotion_procut[$_op['product_id']])){
    //                     $_op['items']=$items;
    //                     $promotion_procut[$_op['product_id']]=$_op;
    //                 }
    //                 if(!isset($item)){
    //                     $item['promotion']=$items;
    //                 }
    //                 break;
    //             case 'base':
    //                 $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
    //                     ->leftJoin('supply_item','order_product_base_item.item_id','=','supply_item.id')
    //                     ->leftJoin('supply_item_non_standard','order_product_base_item.item_id','=','supply_item_non_standard.id')
    //                     ->select('order_product_base_item.*','supply_item.subject as sis','supply_item_non_standard.subject as sinss')
    //                     ->where('type',1)
    //                     ->get()->toArray();
    //                 foreach ($opp as $key=>$_item) {
    //                     $opp[$key]['shop_name']=$shop_name->name;
    //                     if($_item['type']==1){
    //                         if($_item['item_type']==0){
    //                             unset($opp[$key]['sinss']);
    //                             $opp[$key]['subject']=$opp[$key]['sis'];
    //                             unset($opp[$key]['sis']);
    //                         }else{
    //                             unset($opp[$key]['sis']);
    //                             $opp[$key]['subject']=$opp[$key]['sinss'];
    //                             unset($opp[$key]['sinss']);
    //                         }
    //                     }else{
    //                         unset($opp[$key]['sinss']);
    //                         unset($opp[$key]['sis']);
    //                     }
    //                     if(isset($base_procut[$_op['product_id']])){
    //                         $base_procut[$_op['product_id']]['items'][]=$opp[$key];
    //                     }
    //                     if($item){
    //                         $item['base'][]=$opp[$key];
    //                     }
    //                 }
    //                 if(!isset($base_procut[$_op['product_id']])){
    //                     $_op['items']=$opp;
    //                     $base_procut[$_op['product_id']]=$_op;

    //                 }
    //                 if(!isset($item)){
    //                     $item['base']=$opp;
    //                 }
    //                 break;
    //             case 'non_standard':
    //                 $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
    //                     ->leftJoin('supply_item','order_product_non_standard.item_id','=','supply_item.id')
    //                     ->leftJoin('supply_item_non_standard','order_product_non_standard.item_id','=','supply_item_non_standard.id')
    //                     ->select('order_product_non_standard.*','supply_item.subject as sis','supply_item_non_standard.subject as sinss')
    //                     ->where('order_product_non_standard.status',1)
    //                     ->where('service_type',1)
    //                     ->get()->toArray();
    //                 foreach ($opn as $key=>$value) {
    //                     $opn[$key]['shop_name']=$shop_name->name;
    //                     //判断产品项是非标还是标准配件
    //                     if($value['service_type']==1){
    //                         if($value['type']==0){
    //                             unset($opn[$key]['sinss']);
    //                             $opn[$key]['subject']=$opn[$key]['sis'];
    //                             unset($opn[$key]['sis']);
    //                         }else{
    //                             unset($opn[$key]['sis']);
    //                             $opn[$key]['subject']=$opn[$key]['sinss'];
    //                             unset($opn[$key]['sinss']);
    //                         }

    //                     }else{
    //                         unset($opn[$key]['sinss']);
    //                         unset($opn[$key]['sis']);
    //                     }
    //                     if(isset($item)){
    //                         $item['non_standard'][]=$opn[$key];
    //                     }
    //                 }
    //                 if(!$item){
    //                     $item['non_standard']=$opn;
    //                 }

    //         }
    //     }
    //     return array("promotion_procut"=>$promotion_procut,"base_procut"=>$base_procut,"item"=>$item);
    // }

    /**
     * @Author:      zwh
     * @DateTime:    2016-3-7 17:45:15
     * @Description: 维修记录(配件)
     */
    public static function  getOrderProductByOrderIds($order_ids){

        $allItem=$promotion_procut=$base_procut=$item=$is=array();
        $order_ids = array_column($order_ids, 'order_id');
        $order_products=OrderProduct::whereIn("order_id",$order_ids)->where("status",1)->get()->toArray();
        foreach ($order_products as $_op) {
            $order=Orders::find($_op['order_id']);
            $ms=MaintenanceShops::find($order->shop_id);
            $me=MaintenanceEngineers::find($order->engineer_id);
            switch ($_op['product_type']) {
                case 'enterprise':
                    $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opei=OrderProductEsItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opei as $key=>$_item) {
                        // if($_item->item_type==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            $_item->subject=$item_info->subject;
                            $_item->unit=$item_info->unit;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                            $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                            $_item->mileage=$order->mileage;
                            $_item->engineer_name=isset($me->name) ? $me->name : '无';
                            $_item->item_name=$item_info->name;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     $_item->subject=$item_info->subject;
                        //     $_item->unit=$item_info->unit;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        //     $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        //     $_item->mileage=$order->mileage;
                        //     $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        // }
                        $allItem[]=$_item;
                        $item[$opei[$key]['subject']][]=$opei[$key];
                    }
                    break;
                case 'activity':
                    $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opaci=OrderProductAcItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opaci as $key=>$_item) {
                        // if($_item->item_type==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            $_item->subject=$item_info->subject;
                            $_item->unit=$item_info->unit;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                            $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                            $_item->mileage=$order->mileage;
                            $_item->engineer_name=isset($me->name) ? $me->name : '无';
                            $_item->item_name=$item_info->name;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     $_item->subject=$item_info->subject;
                        //     $_item->unit=$item_info->unit;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        //     $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        //     $_item->mileage=$order->mileage;
                        //     $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        // }
                        $allItem[]=$_item;
                        $item[$opaci[$key]['subject']][]=$opaci[$key];
                    }
                    break;
                case 'promotion':
                    $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $items=OrderProductItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get()->toArray();
                    foreach ($items as $key=>$_item) {
                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                        $_item->scf_name=$item_info->scf_name;
                        $_item->unit=$item_info->unit;
                        // $_item->subject_name=$item_info->subject_name;
                        $_item->brand_name=$item_info->brand_name;
                        $_item->series_name=$item_info->series_name;
                        $_item->model=$item_info->model;
                        $_item->specification=$item_info->specification;
                        $_item->mileage=$order->mileage;
                        $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        $_item->item_name=$item_info->name;

                        // $si=SupplyItem::find($_item['item_id']);
                        // $sb=SupplyBrand::find($si->brand_id);
                        // $ss=SupplySeries::find($si->series_id);
                        // $_item['unit']=$ss->unit;
                        // $_item['brand_name']=$sb->brand_name;
                        // $_item['series_name']=$ss->name;
                        // $_item['model']=$si->model;
                        $item[$items[$key]['subject']][]=$items[$key];
                        $allItem[]=$_item;
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
                        // ->leftJoin('supply_item','order_product_base_item.item_id','=','supply_item.id')
                        // ->leftJoin('supply_item_non_standard','order_product_base_item.item_id','=','supply_item_non_standard.id')
                        // ->leftJoin('supply_brand','supply_brand.id','=','supply_item.brand_id')
                        // ->leftJoin('supply_series','supply_series.id','=','supply_item.series_id')
                        // ->select('order_product_base_item.*','supply_item.model as s_model','supply_item.subject as sis','supply_brand.brand_name as s_brand_name','supply_series.name as s_name','supply_item_non_standard.model','supply_item_non_standard.subject as sinss','supply_item_non_standard.brand_name','supply_item_non_standard.series_name')
                        ->where('type',1)
                        ->get();
                    foreach ($opp as $key=>$_item) {
                        // if($_item['item_type']==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            // $_item->subject_name=$item_info->subject_name;
                            $_item->subject=$item_info->subject;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                            $_item->mileage=$order->mileage;
                            $_item->engineer_name=isset($me->name) ? $me->name : '无';
                            $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                            $_item->item_name=$item_info->name;

                            // unset($opp[$key]['sinss']);
                            // $opp[$key]['subject']=$opp[$key]['sis'];
                            // unset($opp[$key]['sis']);
                            // $opp[$key]['brand_name']=$opp[$key]['s_brand_name'];
                            // unset($opp[$key]['s_brand_name']);
                            // $opp[$key]['model']=$opp[$key]['s_model'];
                            // unset($opp[$key]['s_model']);
                            // $opp[$key]['series_name']=$opp[$key]['s_name'];
                            // unset($opp[$key]['s_name']);
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     // $_item->subject_name="";
                        //     $_item->subject=$item_info->subject;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        //     $_item->mileage=$order->mileage;
                        //     $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        //     $_item->shop_name=isset($ms->name) ? $ms->name : '无';

                        //     // unset($opp[$key]['sis']);
                        //     // $opp[$key]['subject']=$opp[$key]['sinss'];
                        //     // unset($opp[$key]['sinss']);
                        //     // unset($opp[$key]['s_brand_name']);
                        //     // unset($opp[$key]['s_name']);
                        //     // unset($opp[$key]['s_model']);
                        // }
                        $item[$opp[$key]['subject']][]=$opp[$key];
                        $allItem[]=$_item;
                    }
                    break;
                case 'non_standard':
                    $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
                        ->where('service_type',1)
                        ->where('order_product_non_standard.status',1)
                        ->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        // if($value['type']==0){
                            $item_info=SupplyItem::getCacheItemInfo($value->item_id);
                            $value->scf_name=$item_info->scf_name;
                            // $value->subject_name=$item_info->subject_name;
                            $value->subject=$item_info->subject;
                            $value->brand_name=$item_info->brand_name;
                            $value->series_name=$item_info->series_name;
                            $value->model=$item_info->model;
                            $value->specification=$item_info->specification;
                            $value->mileage=$order->mileage;
                            $value->engineer_name=isset($me->name) ? $me->name : '无';
                            $value->shop_name=isset($ms->name) ? $ms->name : '无';
                            $value->item_name=$item_info->name;

                            // unset($opn[$key]['sinss']);
                            // $opn[$key]['subject']=$opn[$key]['sis'];
                            // unset($opn[$key]['sis']);
                            // $opn[$key]['brand_name']=$opn[$key]['s_brand_name'];
                            // unset($opn[$key]['s_brand_name']);
                            // $opn[$key]['model']=$opn[$key]['s_model'];
                            // unset($opn[$key]['s_model']);
                            // $opn[$key]['series_name']=$opn[$key]['s_name'];
                            // unset($opn[$key]['s_name']);

                        // }else{

                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($value->item_id);

                        //     $value->scf_name=$item_info->scf_name;
                        //     // $value->subject_name="";
                        //     $value->subject=$item_info->subject;
                        //     $value->brand_name=$item_info->brand_name;
                        //     $value->series_name=$item_info->series_name;
                        //     $value->model=$item_info->model;
                        //     $value->specification=$item_info->specification;
                        //     $value->mileage=$order->mileage;
                        //     $value->engineer_name=isset($me->name) ? $me->name : '无';
                        //     $value->shop_name=isset($ms->name) ? $ms->name : '无';
                        //     // unset($opn[$key]['sis']);
                        //     // $opn[$key]['subject']=$opn[$key]['sinss'];
                        //     // unset($opn[$key]['sinss']);
                        //     // unset($opn[$key]['s_brand_name']);
                        //     // unset($opn[$key]['s_name']);
                        //     // unset($opn[$key]['s_model']);
                        // }
                        $item[$opn[$key]['subject']][]=$opn[$key];
                        $allItem[]=$value;
                    }
                    break;
                case 'service_card':
                    $opp=OrderProductScItem::where("order_product_id",$_op['id'])
                        ->where('type',1)
                        ->get();
                    foreach ($opp as $key=>$_item) {
                        // if($_item['item_type']==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            // $_item->subject_name=$item_info->subject_name;
                            $_item->subject=$item_info->subject;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                            $_item->mileage=$order->mileage;
                            $_item->engineer_name=isset($me->name) ? $me->name : '无';
                            $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                            $_item->item_name=$item_info->name;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     // $_item->subject_name="";
                        //     $_item->subject=$item_info->subject;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        //     $_item->mileage=$order->mileage;
                        //     $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        //     $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        // }
                        $item[$opp[$key]['subject']][]=$opp[$key];
                        $allItem[]=$_item;
                    }
                    break;
            }
        }
        foreach($item as $key=>$_item){
            $ss=SupplyClassification::find($key);
            if(empty($ss)){
                continue;
            }
            $items=$item[$key];
            $updated_at=array();
            foreach($items as $k=>$val){
                $updated_at[$k]=$val['updated_at'];
            }
            array_multisort($updated_at, SORT_DESC, $items);
            $i['id']=$ss->id;
            $i['name']=$ss->name;
            $i['items']=$items;
            $is[]=$i;
        }
        $updated_at1=array();
        foreach($allItem  as $k1=>$val1){
            $updated_at1[$k1]=$val1['updated_at'];
        }
        array_multisort($updated_at1, SORT_DESC, $allItem);
        return array("maintenance_info"=>$is,"allItem"=>$allItem);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-3-7 17:45:15
     * @Description: 维修记录(工时)
     */
    public static function  getOrderHoursByOrderIds($order_ids){
    
        $allHour=array();
        $order_ids = array_column($order_ids, 'order_id');
        $order_products=OrderProduct::whereIn("order_id",$order_ids)->where("status",1)->get()->toArray();
        foreach ($order_products as $_op) {
            $order=Orders::find($_op['order_id']);
            $ms=MaintenanceShops::find($order->shop_id);
            $me=MaintenanceEngineers::find($order->engineer_id);
            switch ($_op['product_type']) {
                case 'enterprise':
                    $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opei=OrderProductEsItem::where('type',2)->whereIn('order_product_package_id',$opp_ids)->select('id','order_id','amount','item_name','counter_amount','updated_at')->get();
                    foreach ($opei as $key=>$_item) {
                        $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        $_item->mileage=$order->mileage;
                        $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        $_item->product_name=$_op['product_name'];
                        $allHour[]=$_item;
                    }
                    break;
                case 'activity':
                    $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opaci=OrderProductAcItem::where('type',2)->whereIn('order_product_package_id',$opp_ids)->select('id','order_id','amount','item_name','counter_amount','updated_at')->get();
                    foreach ($opaci as $key=>$_item) {
                        $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        $_item->mileage=$order->mileage;
                        $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        $_item->product_name=$_op['product_name'];
                        $allHour[]=$_item;
                    }
                    break;
                case 'promotion':
                    $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $items=OrderProductItem::where('type',2)->whereIn('order_product_package_id',$opp_ids)->select('id','order_id','amount','item_name','counter_amount','updated_at')->get()->toArray();
                    foreach ($items as $key=>$_item) {
                        $_item->mileage=$order->mileage;
                        $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        $_item->product_name=$_op['product_name'];
                        $allHour[]=$_item;
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
                    ->where('type',2)
                    ->select('id','order_id','amount','item_name','counter_amount','updated_at')
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        $_item->mileage=$order->mileage;
                        $_item->engineer_name=isset($me->name) ? $me->name : '无';
                        $_item->shop_name=isset($ms->name) ? $ms->name : '无';
                        $_item->product_name=$_op['product_name'];
                        $allHour[]=$_item;
                    }
                    break;
                case 'non_standard':
                    $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
                    ->where('service_type',2)
                    ->where('order_product_non_standard.status',1)
                    ->select('id','order_id','amount','counter_amount','name','updated_at')
                    ->get();
                    foreach ($opn as $key=>$value) {
                        $value->item_name=$value->name;
                        $value->mileage=$order->mileage;
                        $value->engineer_name=isset($me->name) ? $me->name : '无';
                        $value->shop_name=isset($ms->name) ? $ms->name : '无';
                        $value->product_name=$_op['product_name'];
                        $allHour[]=$value;
                    }
                    break;
                case 'service_card':
                    $opn=OrderProductScItem::where("order_product_id",$_op['id'])
                    ->where('type',2)
                    ->select('id','order_id','amount','counter_amount','item_name','updated_at')
                    ->get();
                    foreach ($opn as $key=>$value) {
                        $value->item_name=$value->name;
                        $value->mileage=$order->mileage;
                        $value->engineer_name=isset($me->name) ? $me->name : '无';
                        $value->shop_name=isset($ms->name) ? $ms->name : '无';
                        $value->product_name=$_op['product_name'];
                        $allHour[]=$value;
                    }
                    break;
            }
        }
        $updated_at=array();
        foreach($allHour  as $k=>$val){
            $updated_at[$k]=$val['updated_at'];
        }
        array_multisort($updated_at, SORT_DESC, $allHour);
        return $allHour;
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-6-28 14:20:12
     * @Description: 添加企业包
     */
    public static function implement($pids,$sc_user,$orders,$wmcf,$whp,$es,$csi){
        return DB::transaction(function () use ($pids,$sc_user,$orders,$wmcf,$whp,$es,$csi) {
    
            $order_product=new OrderProduct;
            $order_product->order_id=$orders->order_id;
            $order_product->product_type="enterprise";
            $order_product->product_id=$es->id;
            $order_product->product_name=$es->name;
            $order_product->status=1;
            $order_product->amount=$es->amount;
            $order_product->save();
            $order=Orders::where("order_id",$orders->order_id)->lockForUpdate()->first();
            foreach($pids as $pi){
                $package=ProductPackage::find($pi);
                if(is_null($package)){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_error_50001");
                    return $response;
                }
                if($package->status!=1){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_status_error_50002");
                    return $response;
                }
                // if($package->place!=2||$package->city!=$sc_user->shop->city_id){
                //     DB::rollback();
                //     $response = Lang::get("scs_error.package_unable_error_50003");
                //     return $response;
                // }
                if($package->place!=2){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_unable_error_50003");
                    return $response;
                }
                $p_item=ProductPackage::getItem($orders->car_model_id,$package->id);
    
                $count_amount=0;
                $supply_arr=$p_item['supply'];
                $order_product_item=array();
                foreach ($supply_arr as $_sa) {
                    //更换耗材的加价系数
                    // $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    // if($wpc){
                    //     $wmcf->supply_coefficient=$wpc->coefficient;
                    // }else{
                    //     $wmcf->supply_coefficient=1;
                    // }
                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    $wmcf->supply_coefficient = null;
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                    if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                        $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                    }
                    //更换多城市价格
                    // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $_sa["amount"]=$sip->price;
                    // }
                    $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                    if($sip){
                        $_sa["amount"]=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                        $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                    }

                    $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                    $temp_amount=Common::upFow($temp_amount);
                    $order_product_item[]=array(
                        "item_id"=>$_sa["id"],
                        "item_name"=>$_sa["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_sa["number"],
                        "type"=>1,
                        "unit"=>$_sa["unit"],
                        "is_adapter"=>$_sa["is_adapter"],
                    );
                    $count_amount+=$temp_amount*$_sa["number"];
                }
                foreach ($p_item['server'] as $_se) {
                    $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$sc_user->shop->city_id);
                    if($res['code']==0){
                        $temp_amount=$res['price'];
                    }else{
                    //判断工时类型为工时类型还是固定价格
                        if($_se['type'] ==1 ){
                            $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                        }elseif($_se['type']==2){
                            $temp_amount=$_se["price"];
                        }
                    }
                    $order_product_item[]=array(
                        "item_id"=>$_se["id"],
                        "item_name"=>$_se["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_se["work_hours"],
                        "type"=>2,
                        "unit"=>"个",
                        "is_adapter"=>$_se["is_adapter"]
                    );
                    $count_amount+=$temp_amount;
                }
    
    
                $opp=new OrderProductEsPackage;
                $opp->order_id=$orders->order_id;
                $opp->order_product_id=$order_product->id;
                $opp->package_id=$pi;
                $opp->package_name=$package->name;
                $opp->work_station=0;
                $opp->work_status='prepare';
                $opp->amount=$count_amount;
                $opp->engineer_id=0;
                $opp->save();
    
    
    
                //添加各个产品项
                foreach ($order_product_item as $opi) {
                    $opesi=new OrderProductEsItem;
                    $opesi->order_id=$orders->order_id;
                    $opesi->order_product_package_id=$opp->id;
                    $opesi->item_id=$opi["item_id"];
                    $opesi->item_name=$opi["item_name"];
                    $opesi->amount=$opi["amount"];
                    $opesi->num=$opi["num"];
                    $opesi->type=$opi["type"];
                    $opesi->item_type=0;
                    $opesi->unit=$opi["unit"];
                    $opesi->is_adapter=$opi["is_adapter"];
                    $opesi->save();
                    //判断特殊油品
                    if($opi["type"]==1){
                        if(in_array($opi["item_id"],Config::get("smy.item_id"))){//如果新耗材是特殊油品
                            OrderSmyFilling::saveosf($order->shop_id,$opi["item_id"],"enterprise",$opesi->id,$order->order_id);
                        } 
                    }
                }
            }
            //记录添加日志
            $opr=new OrderProductRecord();
            $opr->order_id=$orders->order_id;
            $opr->product_type="enterprise";
            $opr->operation_type='create';
            $opr->num=1;
            $opr->operation_grade='一级';
            $opr->user_id=$sc_user->id;
            $opr->user_type='sc';
            $opr->operation_id=$order_product->id;
            $opr->name=$order_product->product_name;
            $opr->amount=$order_product->amount;
            $opr->save();
    
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($orders->order_id);
            return $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        });
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-11 14:11:16
     * @Description: 虚拟单添加活动包
     */
    public static function addProductActivity($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi){
        return DB::transaction(function () use ($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi) {
    
            $order_product=new OrderProduct;
            $order_product->order_id=$orders->order_id;
            $order_product->product_type="activity";
            $order_product->product_id=$pa->id;
            $order_product->product_name=$pa->name;
            $order_product->is_edit=$pa->is_edit;
            $order_product->status=1;
            if($pa->type==1){
                $order_product->amount=$pa->price;
            }else{
                $order_product->amount=0;
            }
            $order_product->save();
            $order=Orders::where("order_id",$orders->order_id)->lockForUpdate()->first();
            $total=0;
            foreach($pids as $pi){
                $count_amount=0;
                $package=ProductPackage::find($pi['package_id']);
                if(is_null($package)){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_error_50001");
                    return $response;
                }
                if($package->status!=1){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_status_error_50002");
                    return $response;
                }
                if($package->place!=2){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_unable_error_50003");
                    return $response;
                }
                $p_item=ProductPackage::getItem($orders->car_model_id,$package->id);
                $supply_arr=$p_item['supply'];
                $order_product_item=array();
                foreach ($supply_arr as $_sa) {

                    $papi = ProductActivityPackageItem::where("subject",$_sa["subject"])->where("product_activity_package_id",$pi['id'])->first();
                    if (!empty($papi)) {
                        $item_info = SupplyItem::getCacheItemInfo($papi->supply_item_id);
                        $_sa['number'] = $papi->number;
                        $_sa['name'] = $item_info->name;
                        $_sa['id'] = $papi->supply_item_id;
                        $_sa['subject'] = $item_info->subject;
                        $_sa['brand_id'] = $item_info->brand_id;
                        $_sa['amount'] = $item_info->amount;
                        $_sa['series_name'] = $item_info->series_name;
                        $_sa['model'] = $item_info->model;
                        $_sa['unit'] = $item_info->unit;
                        $_sa['is_adapter'] = 1;
                    }

                    //更换耗材的加价系数

                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    $wmcf->supply_coefficient = null;
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                    if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                        $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                    }

                    //更换多城市价格
                    // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $_sa["amount"]=$sip->price;
                    // }
                    $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                    if($sip){
                        $_sa["amount"]=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                        $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
    
                    $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                    $temp_amount=Common::upFow($temp_amount);
                    $order_product_item[]=array(
                        "item_id"=>$_sa["id"],
                        "item_name"=>$_sa["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_sa["number"],
                        "type"=>1,
                        "unit"=>$_sa["unit"],
                        "is_adapter"=>$_sa["is_adapter"]
                    );
                    $count_amount+=$temp_amount*$_sa["number"];
                    $total+=$temp_amount*$_sa["number"];
                }
                foreach ($p_item['server'] as $_se) {
                    $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$sc_user->shop->city_id);
                    if($res['code']==0){
                        $temp_amount=$res['price'];
                    }else{
                        //判断工时类型为工时类型还是固定价格
                        if($_se['type'] ==1 ){
                            $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                        }elseif($_se['type']==2){
                            $temp_amount=$_se["price"];
                        }
                    }
                    $order_product_item[]=array(
                        "item_id"=>$_se["id"],
                        "item_name"=>$_se["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_se["work_hours"],
                        "type"=>2,
                        "unit"=>"个",
                        "is_adapter"=>1
                    );
                    $count_amount+=$temp_amount;
                    $total+=$temp_amount;
                }
    
    
                $opp=new OrderProductAcPackage;
                $opp->order_id=$orders->order_id;
                $opp->order_product_id=$order_product->id;
                $opp->package_id=$pi['package_id'];
                $opp->package_name=$package->name;
                $opp->work_station=0;
                $opp->work_status='prepare';
                $opp->amount=$count_amount;
                $opp->engineer_id=0;
                $opp->save();
    
    
    
                //添加各个产品项
                foreach ($order_product_item as $opi) {
                    $opaci=new OrderProductAcItem;
                    $opaci->order_id=$orders->order_id;
                    $opaci->order_product_package_id=$opp->id;
                    $opaci->item_id=$opi["item_id"];
                    $opaci->item_name=$opi["item_name"];
                    $opaci->amount=$opi["amount"];
                    $opaci->num=$opi["num"];
                    $opaci->type=$opi["type"];
                    $opaci->item_type=0;
                    $opaci->unit=$opi["unit"];
                    $opaci->discount=$pa->discount;
                    $opaci->is_adapter=$opi['is_adapter'];
                    $opaci->save();
                    //判断特殊油品
                    if($opi["type"]==1){
                        if(in_array($opi["item_id"],Config::get("smy.item_id"))){//如果新耗材是特殊油品
                            OrderSmyFilling::saveosf($order->shop_id,$opi["item_id"],"activity",$opaci->id,$order->order_id);
                        } 
                    }
                }
            }
            
            $order_product->save();
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            Orders::calculate($orders->order_id);
            return $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        });
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-11-24 16:26:35
     * @Description: 判断订单是否使用了优惠券
     */
    public static function isUseCoupons($order_id){
        $op=OrderProduct::where('order_id',$order_id)->where('product_type','base')->where('status',1)->where('coupon_discount','>',0)->count();
        if($op>0){
            return true;
        }
        return false;
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-5 18:41:02
     * @Description: 判断单个包是否使用了优惠券
     */
    public static function isUseCoupon($order_id,$order_product_id){
        $op=OrderProduct::find($order_product_id);
        if($op->coupon_discount>0){
            return true;
        }
        return false;
    }

    /**
     * @Author:      kc
     * @DateTime:    2017-03-29 18:41:02
     * @Description: 以下3个方法是apims合并来的
     */
    public static function getOrderProductMs($order_id){
        $orderProduct = array();
        $activityProduct = array();
        $order_product = OrderProduct::where('order_id',$order_id)
            ->where('status',1)
            ->orderBy('product_type', 'desc')
            ->get();
        foreach($order_product as $product){
            if($product->product_type == 'base'){//基础产品
                $base_item = OrderProduct::getOrderBaseProduct($order_id,$product->id);
                $product->amount = bcdiv($product->amount,100,2);
                $product->counter_amount = bcdiv($product->counter_amount,100,2);
                $product->discount = bcdiv($product->discount,100,2);
                $base = array(
                    'product_id' => $product->product_id,
                    'product_name' => $product->product_name,
                    'packages' =>$base_item,
                    'amount' =>$product,
                );
                array_push($orderProduct,$base);
            }
            if($product->product_type == 'non_standard'){//非标产品
                $non_standard = OrderProduct::getOrderProductNonStandardMs($order_id,$product->id);
                $product->amount = bcdiv($product->amount,100,2);
                $product->counter_amount = bcdiv($product->counter_amount,100,2);
                $product->discount = bcdiv($product->discount,100,2);
                $standard = array(
                    'product_id' => $product->product_id,
                    'product_name' => $product->product_name,
                    'packages' =>$non_standard,
                    'amount' =>$product,
                );
                array_push($orderProduct,$standard);
            }
            if($product->product_type == 'activity'){//活动包
                $activity_bag = OrderProduct::getOrederProductAcitem($order_id,$product->id);
                $product->amount = bcdiv($product->amount,100,2);
                $product->counter_amount = bcdiv($product->counter_amount,100,2);
                $product->discount = bcdiv($product->discount,100,2);
                $activity = array(
                    'product_id' => $product->product_id,
                    'product_name' => $product->product_name,
                    'activity' =>$activity_bag,
                    'amount' =>$product,
                );
                array_push($activityProduct,$activity);
            }
        }
        $data = array(
            'order_product' => $orderProduct,
            'activity_product' => $activityProduct,
        );
        return $data;
    }
    public static function getOrderBaseProduct($order_id,$product_id){
        $base_item = OrderProductBaseItem::where('order_id',$order_id)
            ->where('order_product_package_id',$product_id)
            ->select(['id','amount','num','item_id','item_name as name','type','unit','discount'])
            ->get();
        foreach($base_item as $item){
            $item->amount = bcdiv($item->amount,100,2);
            $item->discount_amount = bcsub($item->amount,bcmul($item->amount,$item->discount,2),2);
        }
        return $base_item;
    }
    public static function getOrderProductNonStandardMs($order_id,$product_id){
        $standard = OrderProductNonStandard::where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->where('status',1)
            ->select(['id','name','amount','num','work_station','work_status','type as standard_type','service_type','unit','discount'])
            ->get();
        foreach($standard as $value){
            $value->amount = bcdiv($value->amount,100,2);
            $value->discount_amount = bcsub($value->amount,bcmul($value->amount,$value->discount,2),2);
        }
        return $standard;
    }
    public static function getOrederProductAcitem($order_id,$product_id){
        $activity_bag = OrderProductAcPackage::with('item')
            ->where('order_id',$order_id)
            ->where('order_product_id',$product_id)
            ->get();
        foreach($activity_bag as $v){
            $v->amount = bcdiv($v->amount,100,2);
            if(!empty($v->item)){
                foreach($v->item as $key => $value){
                    $value->amount = bcdiv($value->amount,100,2);
                    $value->discount_amount = bcsub($value->amount,bcmul($value->amount,$value->discount,2),2);
                }
            }
        }
        return $activity_bag;
    }
}